package com.far;

public class Plane extends Mesh {

	public Plane(){
		this(1,1,1,1);
	}
	
	public Plane(float width, float height){
		this(width, height, 1, 1);
	}
	
	public Plane(float width, float height, int widthSegments, int heightSegments){
		
		//No Of Segments + 1 Vertices Per Line (Segment vertices + starting point) for both X and Y
		//Needs 3 floats to define 1 vertex (x, y, z)
		float[] vertices = new float[(widthSegments + 1) * (heightSegments +1) * 3];
		//2 Triangles Per Segment, Meaning 6 Indices Per Segment
		short[] indices = new short[(widthSegments) * (heightSegments) * 6];
		
		float segmentWidth = width / widthSegments;
		float segmentHeight = height / heightSegments;
		
		float xOffset = width / -2;
		float yOffset = height / -2;
		
		int currentVertex = 0;
		int currentIndex = 0;
		
		short w = (short) (widthSegments + 1);
		
		for(int y = 0; y < heightSegments + 1; y++){
			for(int x = 0; x < widthSegments + 1; x++){
				//Position First Vertex Position for X
				vertices[currentVertex] = xOffset + x * segmentWidth;
				//Position Next Vertex Position for Y
				vertices[currentVertex + 1] = yOffset + y * segmentHeight;
				//Position Last Vertex Position for Z
				vertices[currentVertex + 2] = 0;
				
				//Add 3 To Vertex Array to start the next 3
				currentVertex += 3;
				
				int n = (y * (w) + x);
				
				if(y < heightSegments && x < widthSegments){
					//First Triangle
					indices[currentIndex] = (short) n;
					indices[currentIndex + 1] = (short) (n + 1);
					indices[currentIndex + 2] = (short) (n + w);
					
					//Second Triangle
					indices[currentIndex +3] = (short) (n + 1);
					indices[currentIndex +4] = (short) (n + w + 1);
					indices[currentIndex +5] = (short) (n + w);
					
					currentIndex += 6;
				}
			}
		}
		
		setIndices(indices);
		setVertices(vertices);
	}
	
	

}
